package com.mzy.linear_struct.hot100.动态规划;

import java.util.Arrays;

/**
 * @Author: mizy3
 * @Date: 2024/09/07/下午8:42
 * @Description:
 */
public class Pro1143最长公共子序列 {
    public int longestCommonSubsequence(String text1, String text2) {

        int[][] dp = new int[text2.length() + 1][text1.length() + 1];

        for (int i = 1; i < dp.length; i++) {
            for (int j = 1; j < dp[i].length; j++) {
                if (text1.charAt(j - 1) == text2.charAt(i - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }


        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < dp[i].length; j++) {
                System.out.print(dp[i][j] + " ");
            }
            System.out.println();
        }

        return dp[dp.length - 1][dp[0].length - 1];
    }

    public static void main(String[] args) {
        System.out.println(new Pro1143最长公共子序列().longestCommonSubsequence("hofubmnylkra", "pqhgxgdofcvmr"));
    }
}





























